package com.hongdee.common.util;

import com.hongdee.annotation.ExcelData;
import com.hongdee.base.entity.PropertyVo;
import org.springframework.util.StringUtils;

import javax.persistence.Column;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 名  称:
 * 类  型: class
 * 描  述: 通过Column(nullable)注解，判断实体类内属性是否存在空置
 * 路  径:com.hongdee.common.util
 * 作  者:liuze
 * 日  期:2017/2/7 18:08
 */
public final class EntityUtils {
    /**
     *
     */
    private EntityUtils() {

    }

    /**
     * @param obj @Service 注解的实体
     * @return
     * @throws Exception 基类异常
     * @desc 验证实体数据是否有效，判断依据为实体是否有Column(nullable=false)注解
     * @param: obj 需要判断的实体类
     */
    public static void valid(Object obj) throws Exception {
        Field[] fs = obj.getClass().getDeclaredFields();
        Field.setAccessible(fs, true);
        for (Field f : fs) {
            if (f.getName().toLowerCase().equals("id")) {
                if (fieldIsNull(f, obj)) {
                    try {
                        f.set(obj, null);
                    } catch (IllegalAccessException e) {
                        continue;
                    }
                }
                continue;
            }
            Column c = f.getAnnotation(Column.class);
            if (c == null) {
                continue;
            }
            if (c.nullable()) {
                continue;
            }
            if (fieldIsNull(f, obj)) {
                throw new Exception("字段" + f.getName() + "不允许为空！");
            }
        }
    }

    /**
     * @param f   需要判断的属性字段
     * @param obj 该属性所在的实体类
     * @return bool
     * @throws
     * @desc 判断Object内Field值是否为空，如果为空则设置为null
     * @Author 刘泽
     */
    private static boolean fieldIsNull(Field f, Object obj) throws Exception {
        try {
            Object v = f.get(obj);
            if (v == null || StringUtils.isEmpty(v)) {
                return true;
            }
        } catch (IllegalAccessException e) {
            return true;
        }
        return false;
    }


    public static synchronized List<PropertyVo> getExcelDataList(String clsQualifiedName) throws Exception {
        if (StringUtils.isEmpty(clsQualifiedName))
            throw new Exception("实体路径不能为空");
        Class<?> cls;
        try {
            cls = Class.forName(clsQualifiedName);
        } catch (Exception e) {
            throw new Exception("无效的实体类" + clsQualifiedName);
        }
        if (null == cls)
            throw new Exception("无效的实体类名称！");

        List<PropertyVo> propertyList = new ArrayList<PropertyVo>();
        Field[] fs = cls.getDeclaredFields();
        //  Field.setAccessible(fs, true);
        ExcelData c = null;
        PropertyVo pv = null;
        for (Field f : fs) {
            if (f.getName().toLowerCase().equals("id")) {
                continue;
            }
            //先全部加载
            c = f.getAnnotation(ExcelData.class);
            if (c == null) {
                continue;
            }
            pv = new PropertyVo();
            pv.setPropertyName(f.getName());
            pv.setPropertyType(f.getType().getTypeName());
            pv.setDesc(c.value());

            propertyList.add(pv);
        }

        return propertyList;


    }
}
